package top.littlejiang.onlinexam.common.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.littlejiang.onlinexam.common.configuration.TaskConfig;
import top.littlejiang.onlinexam.common.handler.base.BaseExamHandler;
import top.littlejiang.onlinexam.common.utils.ApplicationContextUtils;
import top.littlejiang.onlinexam.entity.OlexamTask;
import top.littlejiang.onlinexam.mapper.OlexamTaskMapper;
import top.littlejiang.onlinexam.service.OlexamExamService;

/**
 * @author chen
 * @Description
 * @since 2021/4/4 12:00
 */
@Component("open_exam")
@Slf4j
public class OpenExamHandler implements BaseExamHandler {

    private OlexamTaskMapper taskMapper;
    private TaskConfig taskConfig;
    private OlexamExamService examService;
    private OlexamTask task;

    @Override
    public boolean initTask(OlexamTask olexamTask) {
        taskMapper = ApplicationContextUtils.getBean(OlexamTaskMapper.class);
        taskConfig = ApplicationContextUtils.getBean(TaskConfig.class);
        examService = ApplicationContextUtils.getBean(OlexamExamService.class);
        task = olexamTask;
        return true;
    }

    @Override
    public void run() {
        System.out.println("---"+task.getDelay()+"----考试开始-------"+ task.getTaskId() +"--");
        log.info("考试{}，开考",task.getFkId());

        examService.updateStatus(task.getFkId(), 1);

        task.setDelay(task.getDelay() + 1);
        taskMapper.updateById(task);
        taskConfig.delete(task.getTaskId());
    }
}
